package logic

import (
	"context"
	"fmt"
	"we-chat-login-sdk/models/cache"
	"we-chat-login-sdk/models/mysql"

	"we-chat-login-sdk/watchrpc/internal/svc"
	"we-chat-login-sdk/watchrpc/watchrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *watchrpc.RegisterRequest) (*watchrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var fuser mysql.User
	if fuser.ID != 0 {
		return nil, fmt.Errorf("此账号已经被注册")
	}
	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	if result != in.SendSmsCode {
		return nil, fmt.Errorf("验证码错误")
	}
	user := mysql.User{
		Account:   in.Account,
		Mobile:    in.Mobile,
		Password:  in.Password,
		HeadImage: in.HeadImage,
		Address:   in.Address,
	}
	mysql.DB.Debug().Create(&user)
	return &watchrpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
